package com.rigsofrods.truckeditor.rig.parser;

import java.text.ParseException;
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;

import com.rigsofrods.truckeditor.rig.Node;
import com.rigsofrods.truckeditor.rig.TruckModel;
import com.rigsofrods.truckeditor.rig.TruckTreeNode;
import com.rigsofrods.truckeditor.rig.Wheel;
import com.rigsofrods.truckeditor.transforms3D.Vec3D;


public class WheelParser implements IRigParser {

	@Override
	public boolean parseLine(String line, TruckModel truck) throws ParseException {
//radius, width, numrays, node1, node2, snode, braked, propulsed, arm, mass,  spring,   damping,   facemat          bandmat
//0.54,   0.40,  12,      35,    36,    9999,  1,      0,         3,   200.0, 800000.0, 4000.0, tracks/wheelface tracks/wheelband1
		double radius, width, mass,  spring,   damping;
		int numrays, braked, propulsed;
		String node1, node2, snode, arm;
		
		Scanner scnNode = new Scanner(line);
		scnNode.useLocale(Locale.US);
		scnNode.useDelimiter("(\\s+|,\\s*)");
		try{
			radius = scnNode.nextDouble();
			width = scnNode.nextDouble();//unused since 0.32
			numrays = scnNode.nextInt();
			node1 = scnNode.next();
			node2 = scnNode.next();
			snode = scnNode.next();
			braked = scnNode.nextInt();
			propulsed = scnNode.nextInt();
			arm = scnNode.next();
			mass = scnNode.nextDouble();
			spring = scnNode.nextDouble();
			damping = scnNode.nextDouble();
		}catch (InputMismatchException e) {
			scnNode.close();
			return false;
		}finally{
			scnNode.close();
		}
		//Check loaded values
		if(radius < 0.01)
			throw new ParseException("Wheel radius is too small!!", 0);
		if(width < 0.01)
			throw new ParseException("Wheel width is too small!!", 0);
		if(numrays < 3 || numrays > 1000)
			throw new ParseException("Illegal number of rays!", 0);
		if(truck.getNode(node1) == null){
			throw new ParseException("Node1: " + node1, 0);
		}
		if(truck.getNode(node2) == null){
			throw new ParseException("Node2: " + node2, 0);
		}
		if(!snode.equals("9999") && truck.getNode(snode) == null){
			throw new ParseException("SNode: " + snode, 0);
		}
		if(braked < 0 || braked > 4)
			throw new ParseException("Braked: " + braked, 0);
		if(propulsed < 0 || propulsed > 2)
			throw new ParseException("Propulsed: " + propulsed, 0);
		if(truck.getNode(arm) == null)
			throw new ParseException("Arm: Node " + arm + " does not exist!", 0);
		if(mass < 0.01)
			throw new ParseException("Wheel mass is too low!", 0);
		if(spring < 0.0)
			throw new ParseException("Spring value must be positive!", 0);
		if(damping < 0.0)
			throw new ParseException("Damping value must be positive!", 0);
		/*new Wheel(truck, radius, width, numrays, node1, node2, snode,
				braked, propulsed, arm, mass, spring, damping);*/
		return true;
	}

	@Override
	public String getErrorMessage() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean requestComments() {
		// TODO Auto-generated method stub
		return false;
	}

}
